package com.ruoyi.framework.datafill;

import cn.hutool.core.util.ClassUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Component
public class FillFieldStrategyContext implements ApplicationContextAware {

    private static Map<String, FillFieldStrategy> strategies;


    public static Optional<FillFieldStrategy> getStrategy(Class<?> type){
        // 只要传入type是 策略提供类型的实现就会选中策略
        return strategies.values().stream()
                .filter(sgy -> ClassUtil.isAssignable(sgy.getType(), type))
                .findFirst();
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        FillFieldStrategyContext.strategies = context.getBeansOfType(FillFieldStrategy.class);
    }
}
